package com.qn.controller;

import cn.hutool.core.date.DateUtil;
import com.qn.result.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.warm.flow.core.entity.Definition;
import org.dromara.warm.flow.core.orm.agent.WarmQuery;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.utils.page.Page;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("/definition")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProcessDefinitionController {

    private final DefService defService;

    /**
     * 部署流程定义.
     *
     * @return 流程key
     */
    @PostMapping("/deploy")
    public ResponseEntity<Object> createProcessDef() throws FileNotFoundException {
        String categoryLeave = "LEAVE";
        String nameLeave = "请假流程";

        String path = "/Users/quning/IdeaProjects/demo/spring-boot-demo/flow-warm-demo/src/main/resources/process/请假流程审批.bpmn20.xml";
        Definition definition = defService.importIs(new FileInputStream(path));

        // 返回部署的流程id
        return new ResponseEntity<>(new BaseResponse<>(definition.getId()), HttpStatus.OK);
    }

    /**
     * 获取流程定义列表
     */
    @GetMapping("/list")
    public ResponseEntity<BaseResponse<List<Map<String, Object>>>> getProcessDefList() {
        int pageNum = 0;
        int pageSize = 10;

        FlowDefinition condition = new FlowDefinition();
        Page<Definition> page = new WarmQuery<>(defService)
                .orderByCreateTime()
                .desc()
                .page(condition, new Page<>(pageNum, pageSize));

        List<Map<String, Object>> list = new ArrayList<>();
        List<Definition> definitionList = page.getList();
        if (!definitionList.isEmpty()) {
            definitionList.forEach(definition -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", definition.getId());
                map.put("name", definition.getFlowName());
                map.put("category", definition.getCategory());
                map.put("code", definition.getFlowCode());
                map.put("deploymentTime", DateUtil.format(definition.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                map.put("version", definition.getVersion());
                list.add(map);
            });
        }
        return new ResponseEntity<>(new BaseResponse<>(list), HttpStatus.OK);
    }

    /**
     * 发布流程定义
     */
    @PostMapping("/publish")
    public ResponseEntity<BaseResponse<String>> publishProcessDef(@RequestParam Long definitionId) {
        boolean publish = defService.publish(definitionId);
        return new ResponseEntity<>(new BaseResponse<>(publish ? "发布成功" : "发布失败"), HttpStatus.OK);
    }

    /**
     * 取消发布
     */
    @PostMapping("/unPublish")
    public ResponseEntity<BaseResponse<String>> unPublishProcessDef(@RequestParam Long definitionId) {
        boolean unPublish = defService.unPublish(definitionId);
        return new ResponseEntity<>(new BaseResponse<>(unPublish ? "取消发布成功" : "取消发布失败"), HttpStatus.OK);
    }

}
